<?php
	abstract class AbstractRatingHelper extends AppHelper
	{
			var $helpers = array('Javascript', 'Html','Form','Cms.Tooltip');
	
			function star($name,$options = array())
			{
				$theme= null;
				if(isset($options['theme']))
				{
					$theme = $options['theme'];
				}
				if(!$theme && cr('Rating.theme'))
				{
					$theme =  cr('Rating.Ui.theme');
				}
				$this->addStarFiles("cyrstal");
				$options['type'] = "select";
				$captions = true;
				if(isset($options['range']))
				{
					$options['options'] = array_combine(range(1,$options['range']),range(1,$options['range']));
					$captions = false;
					unset($options['range']);
				}
				if(!isset($options['id']))
				{
					$options['id'] = $this->Form->domId($name);
				}
				$starRatingId = $options['id']."_wrapper";
				$options['div']['id']= $starRatingId;
				if(!isset($options['label']))
				{
					$options['label'] = null;
				}
				$ratingOptions = array();
				if(isset($options['ratingOptions']))
				{
					$ratingOptions = $options['ratingOptions'];
					unset($options['ratingOptions']);
				}
				if(isset($options['label']))
				{
					$label = $options['label'];
					$options['label'] = false;
				}else
				{
					$label = $name;
				}
				if($label)
				{
					$label = $this->Form->label($name,$label);
				}
				$options['error'] = false;
				$wrapId = $this->Form->domId();
				$input = $this->Form->input($name,$options);
				$error = $this->Form->error($name);
				$out = $label;
				if($captions == true || isset($options['ratingOptions']['captions']))
				{
					unset($options['ratingOptions']['captions']);
					$captions = true;
					$spanId = $options['id'].'_captions';
					$out .=	$this->Html->tag('span',"",array('id'=>$spanId));
					$options['ratingOptions']['captionEl'] = '$("#'.$spanId.'")';
				}
				if(!isset($options['ratingOptions']))
				{
					$options['ratingOptions'] = array();
				}
				$options['ratingOptions'] = am(array("cancelShow"=>false),$options['ratingOptions']);
				$options['ratingOptions']['inputType'] = "select";
				$optionsJson = array();
				foreach($options['ratingOptions'] as $key => $option)
				{
					if(!in_array($key,array("captionEl","callback")))
					{
						$option = '"'.$option.'"';
					}
					$optionsJson[] = "$key:$option";
				}
				$json = '{'.implode(',',$optionsJson).'}';
				$out .= $input;
                $requriedClass = "";
                if(isset($this->Form->fieldset['validates']) && in_array($name,$this->Form->fieldset['validates']))
                {
                  $requriedClass = "required";
                }
				$out = $this->Html->tag('div',$out,array('class'=>"input select star $requriedClass",'id'=>$wrapId));
				$js = <<<END
		\$('#$starRatingId').stars($json);
		\$('#$starRatingId').removeClass("required");
END;
				$this->Javascript->startup($js);
				if($error)
				{
					$out = $out.$error;
				}
				return $out;
			}

			function display($options = array())
			{
				$default = array(
						'rating'=>'',
						'max_stars'=>5,
						'before_text'=>'',
						'after_text'=>'',
						'class'=>'user_rating_star_value',
						'parent_class'=>'user_rating_star_base',
						'tooltip'=>'',
						'tooltipClass'=>'tooltip_'.mt_rand(1,1000),
						'returnTooltip' => true,			
				);

				$options = am($default,$options);
				extract($options);
				$jsOut = "";
				if(!empty($tooltip))
				{
					$jsOut = $this->Tooltip->addQtipTooltip('.'.$tooltipClass,$tooltip,array('show'=>array('when'=>array('event'=>'mouseover')),'hide'=>array('when'=>array('event'=>'mouseout'))),$returnTooltip);
				}
				$starWidth = ($rating/$max_stars)*100;
				$mainStars = $this->Html->tag('div',"",array('class'=>$class,'style'=>"width:$starWidth%"));
				$out = $this->Html->tag('div',$mainStars,array('class'=>$parent_class,'escape'=>false));
				$out = $this->Html->tag('div',$out,array('class'=>$tooltipClass,'escape'=>false));
				$out .= $jsOut;
				return $before_text.$out.$after_text;
			}

			function addStarFiles($theme = null)
			{
				//@todo remove this file.if ui has new version in cms plugin.
				$this->Javascript->link('/rating/js/jquery-ui.custom.js',false);
				$this->Javascript->link('/rating/js/jquery.ui.stars.js',false);
				$cssName = "jquery.ui.stars.css";
				if($theme)
				{
					$cssName = "jquery.ui.".$theme."_stars.css";
				}
				$this->Html->css('/rating/css/'.$cssName,null,array(),false);
			}
		
	} 
?>